package view.CURD;

import library.controller.FrameListener;
import library.po.Borrow;
import library.po.Identity;
import library.po.User;
import library.service.IdentityService;
import library.service.UserService;
import library.service.impl.IdentityServiceImpl;
import library.service.impl.UserServiceImpl;
import view.info.OverviewDialog;
import view.main.MainFrame;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author unknown
 */
public class OverviewUserDialog extends OverviewDialog {
    public OverviewUserDialog(MainFrame owner, String title) {
        super(owner,title);
        listener = owner.getMainListener();
        listener.setOverviewDialog(this);
        this.setTitle(title);
        this.setVisible(true);
        initComponents();
    }

    private void initComponents() {
        adminViewUsersPanel = new JPanel();
        adminUserButtonBar = new JPanel();
        insertUserButton = new JButton();
        delelteUserButton = new JButton();
        updateUserButton = new JButton();
        selectUserButton = new JButton();
        outputUserButton = new JButton();
        userScrollPane = new JScrollPane();
        userTabel = new JTable();

        //======== this ========
        this.setLayout(new BorderLayout());

        //======== mouseClickMenu ========
        rightClickMenu = new JPopupMenu();
        copyItem = new JMenuItem();
        copyItem.setText("复制");
        copyItem.addActionListener(listener);
        copyItem.setActionCommand("CopyInfo");
        rightClickMenu.add(copyItem);
        //======== adminViewUsersPanel ========
        {
            adminViewUsersPanel.setBorder(new EmptyBorder(12, 12, 12, 12));
            adminViewUsersPanel.setBorder (new javax. swing. border. CompoundBorder( new javax .swing .border .TitledBorder (new
            javax. swing. border. EmptyBorder( 0, 0, 0, 0) , "", javax
            . swing. border. TitledBorder. CENTER, javax. swing. border. TitledBorder. BOTTOM, new java
            .awt .Font ("D\u0069alog" ,java .awt .Font .BOLD ,12 ), java. awt
            . Color. red) ,adminViewUsersPanel. getBorder( )) ); adminViewUsersPanel. addPropertyChangeListener (new java. beans.
            PropertyChangeListener( ){ @Override public void propertyChange (java .beans .PropertyChangeEvent e) {if ("\u0062order" .
            equals (e .getPropertyName () )) throw new RuntimeException( ); }} );
            adminViewUsersPanel.setLayout(null);

            //======== adminUserButtonBar ========
            {
                adminUserButtonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                adminUserButtonBar.setLayout(null);

                //---- insertUserButton ----
                insertUserButton.setText("添加");
                insertUserButton.setFont(new Font("\u5e7c\u5706", insertUserButton.getFont().getStyle() & ~Font.BOLD, insertUserButton.getFont().getSize() + 6));
                insertUserButton.addActionListener(listener);
                insertUserButton.setActionCommand("InsertUser");
                adminUserButtonBar.add(insertUserButton);
                insertUserButton.setBounds(0, 0, 105, 40);

                //---- delelteUserButton ----
                delelteUserButton.setText("删除");
                delelteUserButton.setFont(new Font("\u5e7c\u5706", delelteUserButton.getFont().getStyle() & ~Font.BOLD, delelteUserButton.getFont().getSize() + 6));
                delelteUserButton.addActionListener(listener);
                delelteUserButton.setActionCommand("DeleteUser");
                adminUserButtonBar.add(delelteUserButton);
                delelteUserButton.setBounds(160, 0, 105, 40);

                //---- updateUserButton ----
                updateUserButton.setText("修改");
                updateUserButton.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 18));
                updateUserButton.addActionListener(listener);
                updateUserButton.setActionCommand("UpdateUserDialog");
                adminUserButtonBar.add(updateUserButton);
                updateUserButton.setBounds(350, 0, 105, 40);

                //---- selectUserButton ----
                selectUserButton.setText("查询");
                selectUserButton.setFont(new Font("\u5e7c\u5706", Font.PLAIN, 18));
                selectUserButton.addActionListener(listener);
                selectUserButton.setActionCommand("SelectUserDialog");
                adminUserButtonBar.add(selectUserButton);
                selectUserButton.setBounds(525, 0, 105, 40);

                //---- outputUserButton ----
                outputUserButton.setText("\u5bfc\u51fa");
                outputUserButton.setActionCommand("OutputUser");
                outputUserButton.addActionListener(listener);
                outputUserButton.setFont(new Font("\u5e7c\u5706", outputUserButton.getFont().getStyle() & ~Font.BOLD, outputUserButton.getFont().getSize() + 8));
                adminUserButtonBar.add(outputUserButton);
                outputUserButton.setBounds(695, 0, 100, 40);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < adminUserButtonBar.getComponentCount(); i++) {
                        Rectangle bounds = adminUserButtonBar.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = adminUserButtonBar.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    adminUserButtonBar.setMinimumSize(preferredSize);
                    adminUserButtonBar.setPreferredSize(preferredSize);
                }
            }
            adminViewUsersPanel.add(adminUserButtonBar);
            adminUserButtonBar.setBounds(12, 12, 799, adminUserButtonBar.getPreferredSize().height);

            //======== userScrollPane ========
            {
                userTabel = this.getUserTable(userService.selectAllUsers());
                userScrollPane.setViewportView(userTabel);
            }
            adminViewUsersPanel.add(userScrollPane);
            userScrollPane.setBounds(12, 52, 799, 409);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < adminViewUsersPanel.getComponentCount(); i++) {
                    Rectangle bounds = adminViewUsersPanel.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = adminViewUsersPanel.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                adminViewUsersPanel.setMinimumSize(preferredSize);
                adminViewUsersPanel.setPreferredSize(preferredSize);
            }
        }
        panel = adminViewUsersPanel;
        this.add(panel, BorderLayout.CENTER);
        setSize(825, 505);
        setLocationRelativeTo(getOwner());
    }
    public JTable getUserTable(List<User> userList){
        String titles[] = {"姓名", "性别", "用户名", "密码" ,"信用值" ,"身份"};
        Object data[][] = new Object[userList.size()][6];
        Iterator<User> iter = userList.iterator();
        int i = 0;
        while (iter.hasNext()) {
            User user = iter.next();
            data[i][0] = user.getUserName();
            data[i][1] = user.getUserSex();
            data[i][2] = user.getUserId();
            data[i][3] = user.getUserPassword();
            data[i][4] = user.getUserCredit();
            data[i][5] = identityService.selectByType(user.getUserType()).getIdentityName();
            i++;
        }
        DefaultTableModel tableModel = new DefaultTableModel(data,titles);
        JTable table = new JTable(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(900, 600));
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14)); //表头字体
        table.setFont(new Font("微软雅黑", Font.PLAIN, 14)); //表体字体
        table.getTableHeader().setPreferredSize(new Dimension(1, 50)); //表头高度
        table.setRowHeight(table.getRowHeight()+30); //表体高度
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //表格只能选择一行
        table.repaint();
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(final MouseEvent e) {
                int mods=e.getModifiers();
                table.add(rightClickMenu);
                //鼠标右键
                if((mods& InputEvent.BUTTON3_MASK)!=0){
                    //弹出菜单
                    rightClickMenu.show(table, e.getX(), e.getY());
                }
                selRow = table.getSelectedRow();  //得到选中的行
            }
        });
        return table;
    }
    public int getSelRow(){
        return this.selRow;
    }
    public FrameListener getListener(){
        return this.listener;
    }
    public void userRefresh(List<User> userList) {
        this.remove(panel);
        panel.remove(userScrollPane);
        userScrollPane.remove(userTabel);
        userTabel = this.getUserTable(userList);
        userScrollPane.setViewportView(userTabel);
        panel.add(userScrollPane);
        this.add(panel);
        this.validate();
    }
    private JPanel adminViewUsersPanel;
    private JPanel adminUserButtonBar;
    private JButton insertUserButton;
    private JButton delelteUserButton;
    private JButton updateUserButton;
    private JButton selectUserButton;
    private JButton outputUserButton;
    private JScrollPane userScrollPane;
    private JTable userTabel;
    private FrameListener listener;
    private JPopupMenu rightClickMenu;
    private JMenuItem copyItem;
    private IdentityService identityService = new IdentityServiceImpl();
    private UserService userService = new UserServiceImpl();
    private JPanel panel;
    private int selRow = -1;
}
